<template>
  <div class="app-container">
    <div style="margin-right:10px">
      <el-button
        @click="dialogVisible = true"
        type="primary"
        size="mini"
        icon="el-icon-upload">
        导入Excel
      </el-button>
      <el-button
        @click="exportData"
        type="primary"
        size="mini"
        icon="el-icon-download">
        导出Excel
      </el-button>
    </div>

    <el-table :data="list" border row-key="id" lazy :load="load">
      <el-table-column label="名称" align="left" prop="name" />
      <el-table-column label="编码" prop="dictCode" />
      <el-table-column label="值" align="left" prop="value" />
    </el-table>

    <el-dialog
      title="数据字典导入"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form>
        <el-form-item label="轻选Excel文件">
          <!-- accept 只接受Excel文件 -->
          <el-upload
            :auto-upload="false"
            :multiple="false"
            :limit="1"
            :on-exceed="fileUploadExced"
            :on-success="fileUploadSuccess"
            :on-error="fileUploadError"
             :action="BASE_API + '/admin/core/dict/import'"
            name="file"
            accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listByParentId } from '@/api/core/dict'
export default {
  data() {
    return {
      // 是否打开对话框
      dialogVisible: false,
      //获取后端接口地址
      BASE_API: process.env.VUE_APP_BASE_API,
      // 表格数据
      list: [],
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    /* 上传文件回调函数 */
    fileUploadExced() {
      this.$message.warning('只能选择一个文件')
    },
    /* 文件上传成功后回调函数 */
    fileUploadSuccess(response) {
      // 上传成功后，关闭对话框，在页面展示上传的数据
      if(response.code === 0) {
        this.$message.success('数据导入成功')
        this.dialogVisible = false
        this.fetchData()
      } else{
        this.$message.error(response.message)
      }
    },
    /* 文件上传失败回调函数 */
    fileUploadError(error) {
      this.$message.error('数据导入失败')
    },
    /* 导出Excel */
    exportData() {
      window.location.href= this.BASE_API + '/admin/core/dict/export'
    },
    /* 获取数据库中的数据 */
    fetchData() {
      /* 默认查询出全部分类 */
      listByParentId(1).then(response => {
        this.list = response.data.list
      })
    },
    /* 延迟加载子节点
     * row 当前行数据
     * treeNode 节点层级信息
     * resolve 回调函数，处理数据显示 用来解析数据到表格上
     */
    load(row, treeNode, resolve) {
      console.log('row', row)
      console.log('treeNode', treeNode)
      listByParentId(row.id).then(response => {
        // 负责将子节点的数据展示在展开的列表中
        resolve(response.data.list)
      })
    }
  }
}
</script>